using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spwaner : MonoBehaviour
{
    public static Spwaner instance;
    public List<GameObject> bullets = new List<GameObject>();
    public List<GameObject> foods = new List<GameObject>();
    public List<GameObject> barrages = new List<GameObject>();
    public List<float> bulletsGeneratedCount = new List<float>();
    public List<float> bulletsGeneratedInterval = new List<float>();
    public List<float> minBarrageTime = new List<float>();
    public List<float> maxBarrageTime = new List<float>();

    public float foodTime;
    private float countFoodTime = 10;
    private float thisBarrageTime = 0;
    private float countBarrageTime = 0;
    private int count = 1;
    private int bulletCount = 1;

    private bool endFlag = false;



    private Vector3 spwanPosition;

    void Awake()
    {
           instance = this;
    }
    void Start()
    {
        thisBarrageTime = Random.Range(minBarrageTime[count], maxBarrageTime[count]);
        EventManager.GetInstance.StartListening("gameStart", resetSpwaner);
    }
    void Update()
    {
        if (Global.instance.Level < 2) { count = 0; }
        else if (Global.instance.Level < 4) { count = 1; }
        else if (Global.instance.Level < 6) { count = 2; }
        else if (Global.instance.Level < 10) { count = 3; }
        if (endFlag) {
            countBarrageTime += Time.deltaTime;
            if (countBarrageTime >= thisBarrageTime)
            {
                CreateBarrage();
                countBarrageTime = 0;
                thisBarrageTime = Random.Range(0.02f, 0.04f);
            }
        }
        else {
            SpwanBullet();
            SpwanFood();
            SpwanBarrage();
        }
        
    }

    private void OnDestroy()
    {
        EventManager.GetInstance.StopListening("gameStart", resetSpwaner);
    }

    public void resetSpwaner()
    {
        endFlag = false;
    }
    public void HappyEnding()
    {
        endFlag = true;
    }




        public void SpwanBarrage()
    {
        if (Global.instance.TimeCount == 0) { return; }
        countBarrageTime += Time.deltaTime;
        if (countBarrageTime >= thisBarrageTime)
        {
            CreateBarrage();
            countBarrageTime = 0;
            thisBarrageTime = Random.Range(minBarrageTime[count], maxBarrageTime[count]);
        }
    }

    public void CreateBarrage()
    {
        spwanPosition = transform.position + new Vector3(5,0,0);
        spwanPosition.y = Random.Range(-4f, 4.5f);
        Instantiate(barrages[Random.Range(0, barrages.Count)], spwanPosition, Quaternion.identity);
    }

    public void SpwanFood()
    {
        if (Global.instance.TimeCount == 0) { return; }
        countFoodTime += Time.deltaTime;

        if (countFoodTime >= foodTime)
        {
            CreateFood();
            countFoodTime = 0;
        }
    }

    public void CreateFood()
    {
        if (Global.instance.TimeCount <= 14)
        {
            InstantiatePrefab(foods[0]);
        }
        else if (Global.instance.TimeCount <= 28)
        {
            InstantiatePrefab(foods[1]);
        }
        else if (Global.instance.TimeCount <= 42)
        {
            InstantiatePrefab(foods[2]);
        }
        else if (Global.instance.TimeCount <= 56)
        {
            InstantiatePrefab(foods[3]);
        }
        else if (Global.instance.TimeCount <= 70)
        {
            InstantiatePrefab(foods[4]);
        }
    }


    public void SpwanBullet()
    {
        if (Global.instance.TimeCount == 0) { bulletCount = 1; return; }
        else if (Global.instance.TimeCount >=7 && bulletCount == 1) {
            InstantiatePrefab(bullets[0]);
            bulletCount++;
        }
        else if (Global.instance.TimeCount >= 22 && bulletCount == 2)
        {
            InstantiatePrefab(bullets[1]);
            bulletCount++;
        }
        else if (Global.instance.TimeCount >= 37 && bulletCount == 3)
        {
            InstantiatePrefab(bullets[2]);
            bulletCount++;
        }
        else if (Global.instance.TimeCount >= 52 && bulletCount == 4)
        {
            InstantiatePrefab(bullets[3]);
            InstantiatePrefab(bullets[4]);
            
            bulletCount++;
        }
        
    }

    void CreateBullet()
    {
        int index = (int)Global.instance.TimeCount / 20;

        float bulletCount = bulletsGeneratedCount[count];
        for (int i = 1; i <= bulletCount; i++)
        {
            if (Global.instance.TimeCount <= 25) {
                InstantiatePrefab(bullets[Random.Range(0, 2)]);
            }
            else if (Global.instance.TimeCount <= 50) {
                InstantiatePrefab(bullets[Random.Range(2, 4)]);
            }
            else if (Global.instance.TimeCount <= 70)
            {
                InstantiatePrefab(bullets[Random.Range(4, 5)]);
            }

        }
    }

    void InstantiatePrefab(GameObject Prefab)
    {
        spwanPosition = transform.position;
        spwanPosition.y = Random.Range(-4f, 4.5f);
        Instantiate(Prefab, spwanPosition, Quaternion.identity);
    }


}
